<template>
	<uv-loading-page :loading="loading" loading-text="加载中..." color="#d33123" loadingColor="#d33123" font-size="24rpx"
		bgColor="rgba(255, 255, 255, 0.2)"></uv-loading-page>
</template>
<script setup lang="ts">
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { handleRouteSwitchTab, handleRouteRedirectTo, handleShowToast } from '@/core/utils';
	import { queryDeshare, setMyInvite } from '@/core/api';
	import type { Response } from '@/core/models';
	import { Invitecode, UserToken } from '@/core/consts';
	import { ShareEnum, AllowUserSecondBindEnum } from '@/core/enum';
	import { useShopConfigStore } from '@/core/store';

	interface QueryParams {
		scene : string | number;
	}

	/** 获取项目配置 */
	const shopConfigStore = useShopConfigStore();

	const loading = ref(true);

	onLoad((query : QueryParams) => {
		if (query?.scene) {
			handleShare(query?.scene)
		} else {
			handleRouteSwitchTab();
		}
	});

	const handleShare = async (code : string | number) => {
		const shareData : Response<any> = await queryDeshare({ code, });
		if (!shareData.status) {
			loading.value = false;
			handleShowToast('跳转失败', 'error', () => {
				setTimeout(() => {
					handleRouteSwitchTab();
				}, 1000)
			})
			return;
		} 

		/** 登录状态 + 后台开启 设置上下级 */
		if (uni.getStorageSync(UserToken) && shopConfigStore.config?.isAllowUserSecondBind === AllowUserSecondBindEnum.yes) {
			await setMyInvite({ id: shareData.data?.userShareCode });
		}else{
			uni.setStorageSync(Invitecode, shareData.data?.userShareCode);
		}
		
		loading.value = false;
		switch (Number(shareData.data?.page)) {
			/** 首页*/
			case ShareEnum.index:
				handleRouteSwitchTab();
				break;
			/** 商品详情页*/
			case ShareEnum.goods:
				if (shareData.data?.params?.goodsId) {
					handleRouteRedirectTo(`/pages/goods/detail?id=${shareData.data?.params?.goodsId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 拼团详情页*/
			case ShareEnum.pinTuan:
				if (shareData.data?.params?.groupId && shareData.data?.params?.teamId) {
					handleRouteRedirectTo(`/pages/subpackage/activity/pinTuan/detail/detail?id=${shareData.data?.params?.groupId}&teamId=${shareData.data?.params?.teamId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 店铺邀请*/
			case ShareEnum.inv:
				if (shareData.data?.params?.store) {
					handleRouteRedirectTo(`/pages/subpackage/distribution/store/index?store=${shareData.data?.params?.store}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 文章详情页面 */
			case ShareEnum.article:
				if (shareData.data?.params?.articleId) {
					handleRouteRedirectTo(`/pages/subpackage/article/detail/detail?id=${shareData.data?.params?.articleId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 参团页面 */
			case ShareEnum.addPinTuan:
				if (shareData.data?.params?.groupId && shareData.data?.params?.teamId) {
					handleRouteRedirectTo(`/pages/subpackage/activity/pinTuan/detail/detail?id=${shareData.data?.params?.groupId}&teamId=${shareData.data?.params?.teamId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 自定义页面 */
			case ShareEnum.page:
				if (shareData.data?.params?.pageCode) {
					handleRouteRedirectTo(`/pages/subpackage/custom/custom?code=${shareData.data?.params?.pageCode}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 智能表单 */
			case ShareEnum.form:
				if (shareData.data?.params?.id) {
					handleRouteRedirectTo(`/pages/subpackage/form/detail/detail?id=${shareData.data?.params?.id}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 团购 */
			case ShareEnum.group:
				if (shareData.data?.params?.groupId) {
					handleRouteRedirectTo(`/pages/subpackage/activity/groupBuying/detail/detail?id=${shareData.data?.params?.groupId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 秒杀 */
			case ShareEnum.seckill:
				if (shareData.data?.params?.groupId) {
					handleRouteRedirectTo(`/pages/subpackage/activity/seckill/detail/detail?id=${shareData.data?.params?.groupId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 代理商*/
			case ShareEnum.agent:
				if (shareData.data?.params?.store) {
					handleRouteRedirectTo(`/pages/subpackage/agency/store/index?store=${shareData.data?.params?.store}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 接龙*/
			case ShareEnum.solitaire:
				if (shareData.data?.params?.id) {
					handleRouteRedirectTo(`/pages/subpackage/activity/solitaire/detail/detail?id=${shareData.data?.params?.id}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			/** 服务商品*/
			case ShareEnum.serviceGoods:
				if (shareData.data?.params?.serviceGoodsId) {
					handleRouteRedirectTo(`/pages/subpackage/serviceGoods/detail/detail?id=${shareData.data?.params?.serviceGoodsId}`)
				} else {
					handleRouteSwitchTab();
				}
				break;
			default:
				handleRouteSwitchTab();
				break;
		}

	}
</script>
<style lang="scss" scoped></style>